/*
 *  Menu.c
 *  AutoEllips
 *
 *  Created by Simon Martin on 14/05/2010.
 *  Copyright 2010 __MyCompanyName__. All rights reserved.
 *
 */
#include "MainMenu.h"

// menu choices
char *choices[] = {
	"Auto run",
	"Manually acquire data",
	"Input Psi and Delta values",
	"Calculate Psi and Delta",
	"View/Change defaults",
	"Exit"
};

int MainMenu(WINDOW *mainwnd)
//int main()
{	int startx=0;
	int starty=0;
	int n_choices = sizeof(choices) / sizeof(char *);
	
	WINDOW *menu_win;
	int highlight = 1;
	int choice = 0;
	int c;
	
	initscr();
	clear();
	noecho();
	cbreak();	/* Line buffering disabled. pass on everything */
	startx = (80 - WIDTH) / 2;
	starty = (24 - HEIGHT) / 2;
	
	menu_win = newwin(HEIGHT, WIDTH, starty, startx);
	keypad(menu_win, TRUE);
	mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
	refresh();
	print_menu(menu_win, highlight,n_choices);
	while(1)
	{	c = wgetch(menu_win);
		switch(c)
		{	case 65:
				if(highlight == 1)
					highlight = n_choices;
				else
					--highlight;
				break;
			case 66:
				if(highlight == n_choices)
					highlight = 1;
				else 
					++highlight;
				break;
			case 10:
				choice = highlight;
				break;
			default:
				refresh();
				break;
		}
		print_menu(menu_win, highlight,n_choices);
		if(choice != 0)	/* User chose to come out of the infinite loop */
		{
			clrtoeol();
			refresh();
			delwin(menu_win);
			return choice;
			break;
	}	
	
	
}

}



void print_menu(WINDOW *menu_win, int highlight,int n_choices)
{
	int x, y, i;	
	
	x = 2;
	y = 2;
	box(menu_win, 0, 0);
	for(i = 0; i < n_choices; ++i)
	{	if(highlight == i + 1) // Highlight the current choice 
	{	wattron(menu_win, A_REVERSE); 
		mvwprintw(menu_win, y, x, "%s", choices[i]);
		wattroff(menu_win, A_REVERSE);
	}
	else
		mvwprintw(menu_win, y, x, "%s", choices[i]);
		++y;
	}
	wrefresh(menu_win);
}


